import axios from "axios";
import qs from "qs";
// import {
//     Dialog,
//     Toast
// } from "vant";

// Toast
// import { Toast } from 'vant';
// import 'vant/es/toast/style';

// // Dialog
// import { Dialog } from 'vant';
// import 'vant/es/dialog/style';

import { ElMessage, ElMessageBox, ElNotification, ElLoading } from 'element-plus'

// axios.defaults.baseURL = 'http://hplqy.suoluomei.cn/index.php?s=/hfs/Api/'  //正式
// axios.defaults.baseURL = 'http://activitytest.hpl001.cn/crm_api/app/sinceOrder/' //测试
// axios.defaults.baseURL = 'https://www.xiqueer.com/zsyx/wap/' //测试
axios.defaults.baseURL = 'http://zdxkinfo.com.cn/tracking/' //测试

//post请求头
axios.defaults.headers.post["Content-Type"] =
    "application/x-www-form-urlencoded;charset=UTF-8";
//设置超时
axios.defaults.timeout = 10000;

let loading;

axios.interceptors.request.use(
    config => {
        loading = ElLoading.service({
            lock: true,
            text: '加载中...',
            background: 'rgba(0, 0, 0, 0.7)',
        });
        // ElMessage({
        //         message: '请求超时，请稍后再试',
        //         type: 'error'
        //     })
        // Toast.loading({
        //     duration: 0,
        //     message: '加载中...',
        //     forbidClick: true,
        // });
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

axios.interceptors.response.use(
    response => {
        // Toast.clear();
        loading.close();
        return response;
    },
    error => {
        loading.close();
        ElMessage({
                message: '网络请求失败，反馈给客服',
                type: 'error'
            })
            // Toast.clear();
            // Dialog.alert({
            //     title: "提示",
            //     message: "网络请求失败，反馈给客服"
            // });
    }
);

export default function axiosApi(type, params, method) {
    // let sign = process.env.VUE_APP_SIGN
    // if (process.env.NODE_ENV === 'production') {
    //     sign = localStorage.getItem("wx_sign")
    // } else {
    let sign = 'crm:user:sign:f0c8cbe468f6a34463d198268290903f'
        // }
    var value = {
        sign: sign
    }
    var data = method == "post" ? qs.stringify(Object.assign(value, params)) : Object.assign(value, params)
    return new Promise((resolve, reject) => {
        axios({
                method: method,
                url: type,
                // headers: {
                //     // 'x-access-token': 'asdadadasdasdsa'
                //     'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
                // },
                data: data
            })
            .then(res => {
                console.log(res);
                if (res.status == 200) {
                    resolve(res.data)
                } else {
                    // 接口错误提示
                    // Toast.fail(res.data.msg);
                    ElMessage({
                        message: res.data.msg || '接口错误',
                        type: 'error'
                    })
                }
            })
            .catch(err => {
                reject(err)
            });
    })
};